package com.hele.sts.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.hele.sts.dao.QUser;
import com.hele.sts.dao.Qyhznr;
import com.hele.sts.dao.QyhznrDao;
import com.hele.sts.dao.TreeDao;
import com.hele.sts.dao.XtServer;
import com.hele.sts.dao.XtServerDao;
import com.hele.sts.dto.TreeNode;
import com.hele.sts.pub.Constants;
import com.hele.sts.pub.CxqxUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LeftTreeAction implements Action{

	private static final Logger logger = LoggerFactory.getLogger(LeftTreeAction.class);
	private String nbxh;
	private String treeStr;
	private String id;
	private String lv;//level
	private String q;
	//private InputStream inputStream;
	
	@Override
	public String execute() throws Exception {
		if(q==null){
			q="";
		}
		q = q.trim();
		
		if(StringUtils.isNotBlank(id)&&"2".equals(lv)){
				return getTreeNodes(id);
		}
			
			return getTree();
		
	}

	private String getTreeRootNodes() {
		logger.debug("getTreeRootNodes");
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String userId = (String) session.get("userid");

		if (StringUtils.isBlank(userId)) {
			treeStr = "relogin";
			return "ajax";
		}
		String cxqx = (String)session.get("cxqx");
		if(cxqx==null) {
			cxqx = Constants.USER_CXQX_WY;
		}
		List<TreeNode> nodeList = new ArrayList<TreeNode>();
		
		Qyhznr qyhznr = QyhznrDao.load(nbxh);
		if(qyhznr==null){
			qyhznr = QyhznrDao.loadBfTj(nbxh);
		}
		if(qyhznr==null){
			TreeNode treeNode = new TreeNode(nbxh,"0","未知企业",Constants.USER_CXQX_WY,true,true);
			treeNode.setIconSkin("pIcon01");
			nodeList.add(treeNode);
			treeStr = JSON.toJSONString(nodeList);
			return "ajax";
		}
		
		//根节点，企业名称
		TreeNode treeNode = new TreeNode(nbxh,"0",qyhznr.getQymc(),Constants.USER_CXQX_WY,true,true);
		treeNode.setIconSkin("pIcon01");
		nodeList.add(treeNode);
		//正卷
		
		List<TreeNode> zfjList = Lists.newArrayList(Collections2.transform(TreeDao.getZfjList(nbxh), setCanOpen(cxqx)));
		//List<TreeNode> zfjList = Lists.newArrayList(Collections2.filter(TreeDao.getZfjList(nbxh), lowCxqxThan(cxqx)));
		nodeList.addAll(zfjList);
		
		
		List<TreeNode> jnmlList = new ArrayList<TreeNode>();
		for (Iterator iterator = zfjList.iterator(); iterator.hasNext();) {
			TreeNode treeNode1 = (TreeNode) iterator.next();
			//jnmlList.addAll(Collections2.filter(TreeDao.getJnmlList(treeNode1.getId()), lowCxqxThan(cxqx)));
			jnmlList.addAll(Collections2.transform(TreeDao.getJnmlList(treeNode1.getId(),q), setCanOpen(cxqx)));
			
		}
		nodeList.addAll(jnmlList);
		
//		List<TreeNode> inmlList = new ArrayList<TreeNode>();
//		for (Iterator iterator = jnmlList.iterator(); iterator.hasNext();) {
//			TreeNode treeNode1 = (TreeNode) iterator.next();
//			inmlList.addAll(Collections2.filter(TreeDao.getInmlList(treeNode1.getId()), lowCxqxThan(cxqx)));
//			
//		}
//		nodeList.addAll(inmlList);
		
		if (StringUtils.isBlank(nbxh)) {
			treeStr = "";
		}
		treeStr = JSON.toJSONString(nodeList);
		//treeStr = 
		return "ajax";
	}

	private String getTree() {
		logger.debug("getTree,nbxh:"+nbxh);
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String userId = (String) session.get("userid");
		if (StringUtils.isBlank(userId)) {
			return "login";
		}
		String cxqx = (String)session.get("cxqx");
		logger.debug("cxqx:{}",cxqx);
		if(cxqx==null) {
			cxqx = Constants.USER_CXQX_WY;
		}
		List<TreeNode> nodeList = new ArrayList<TreeNode>();
		
		Qyhznr qyhznr = QyhznrDao.load(nbxh);
		if(qyhznr==null){
			qyhznr = QyhznrDao.loadBfTj(nbxh);
		}
		if(qyhznr==null){
			logger.debug("nbxh错误，未找到企业");
			TreeNode treeNode = new TreeNode(nbxh,"0","未知企业",Constants.USER_CXQX_WY,true,true);
			treeNode.setIconSkin("pIcon01");
			nodeList.add(treeNode);
			treeStr = JSON.toJSONString(nodeList);
			return "success";
		}
		
		//根节点，企业名称
		TreeNode treeNode = new TreeNode(nbxh,"0",qyhznr.getQymc(),Constants.USER_CXQX_WY,true,true);
		treeNode.setIconSkin("pIcon01");
		nodeList.add(treeNode);
		
		QUser qUser =  (QUser)session.get("quser");
		logger.debug("groupId:{}",qUser.getGroupId());
		//if(qUser.getGroupId()==null||"1".equals(qUser.getGroupId())||!q.isEmpty()){
		//if(qUser.getGroupId()==null||"1".equals(qUser.getGroupId())||!q.isEmpty()){
			//正卷
			
			List<TreeNode> zfjList = Lists.newArrayList(Collections2.transform(TreeDao.getZfjList(nbxh), setCanOpen(cxqx)));
			//List<TreeNode> zfjList = Lists.newArrayList(Collections2.filter(TreeDao.getZfjList(nbxh), lowCxqxThan(cxqx)));
			
			logger.debug("getTree,zfjList.size:"+zfjList.size()+" cxqx:"+cxqx);
			nodeList.addAll(zfjList);
			
			
			List<TreeNode> jnmlList = new ArrayList<TreeNode>();
			for (Iterator iterator = zfjList.iterator(); iterator.hasNext();) {
				TreeNode treeNode1 = (TreeNode) iterator.next();
				//jnmlList.addAll(Collections2.filter(TreeDao.getJnmlList(treeNode1.getId()), lowCxqxThan(cxqx)));
				jnmlList.addAll(Collections2.transform(TreeDao.getJnmlList(treeNode1.getId(),q), setCanOpen(cxqx)));
				
			}
			logger.debug("getTree,jnmlList.size:"+jnmlList.size()+" cxqx:"+cxqx);
			nodeList.addAll(jnmlList);
		//}
		
		
//		List<TreeNode> inmlList = new ArrayList<TreeNode>();
//		for (Iterator iterator = jnmlList.iterator(); iterator.hasNext();) {
//			TreeNode treeNode1 = (TreeNode) iterator.next();
//			inmlList.addAll(Collections2.filter(TreeDao.getInmlList(treeNode1.getId()), lowCxqxThan(cxqx)));
//			
//		}
//		nodeList.addAll(inmlList);
		
		if (StringUtils.isBlank(nbxh)) {
			treeStr = "";
		}
		treeStr = JSON.toJSONString(nodeList);
		//treeStr = 
		return "success";
	}

	private Function<TreeNode, TreeNode> setCanOpen(final String cxqx) {
		
		return new Function<TreeNode, TreeNode>(){

			@Override
			public TreeNode apply(TreeNode input) {
				boolean res =  CxqxUtil.highCxqx(input.getCxqx(),cxqx);
				input.setCanOpen(res);
				return input;
			}
			
		};
	}

	private String getTreeNodes(String id) {
		logger.debug("getTreeNodes,nbxh:"+nbxh+" id:"+id);
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String userId = (String) session.get("userid");
		if (StringUtils.isBlank(userId)) {
			treeStr = "relogin";
			return "ajax";
		}
		
		
		String cxqx = (String)session.get("cxqx");
		if(cxqx==null) {
			cxqx = Constants.USER_CXQX_WY;
		}
		
//		boolean isDjjgShow = false;
//		
//		String[] str = id.split("_");
//		String nbxh =str[0];
//		Qyhznr qyhznr = QyhznrDao.load(nbxh);
//		if(qyhznr==null){
//			qyhznr = QyhznrDao.loadBfTj(nbxh);
//		}
//		String djjg = qyhznr==null?null:qyhznr.getDjjg();
//		if(djjg!=null){
//			XtServer server = XtServerDao.load(djjg, null, 0);
//			isDjjgShow = Constants.DIR_DJJGSHOW_SHOW.equals(server.getDjjgShow());
//		}
		
		//String djjg = (String)session.get("djjg");
//		
//		
//		
//		boolean isDjjgShow = false;
//		if(djjg!=null){
//			XtServer server = XtServerDao.load(djjg, null, 0);
//			isDjjgShow = Constants.DIR_DJJGSHOW_SHOW.equals(server.getDjjgShow());
//		}
		
		
		
		List<TreeNode> inmlList = new ArrayList<TreeNode>();
//
//		QUser qUser = (QUser)session.get("quser");
//		if("0".equals(qUser.getGroupId())&&"".equals(q)){
//			
//		}else{
//			inmlList.addAll(Collections2.transform(TreeDao.getInmlList(id,q), setCanOpen(cxqx)));
//		}
		logger.debug("getTreeNodes id={}",id);
		inmlList.addAll(Collections2.transform(TreeDao.getInmlList(id,q), setCanOpen(cxqx)));
		treeStr = JSON.toJSONString(inmlList);
		return "ajax";
	}

	/**
	 * @return the nbxh
	 */
	public String getNbxh() {
		return nbxh;
	}

	/**
	 * @param nbxh the nbxh to set
	 */
	public void setNbxh(String nbxh) {
		this.nbxh = nbxh;
	}

	/**
	 * @return the treeStr
	 */
	public String getTreeStr() {
		return treeStr;
	}

	/**
	 * @param treeStr the treeStr to set
	 */
	public void setTreeStr(String treeStr) {
		this.treeStr = treeStr;
	}
	
	private Predicate<TreeNode> lowCxqxThan(final String cxqx){
		return new Predicate<TreeNode>(){

			@Override
			public boolean apply(TreeNode input) {
				return CxqxUtil.highCxqx(input.getCxqx(),cxqx);
			}
			
		};
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the q
	 */
	public String getQ() {
		return q;
	}

	/**
	 * @param q the q to set
	 */
	public void setQ(String q) {
		this.q = q;
	}

	/**
	 * @return the lv
	 */
	public String getLv() {
		return lv;
	}

	/**
	 * @param lv the lv to set
	 */
	public void setLv(String lv) {
		this.lv = lv;
	}

	
}
